﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WooCoo.DataPortal;
using WooCoo.Portal.Contracts;

namespace WooCoo.Portal.WebPortal.Controllers
{
    [AllowAnonymous]
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Index()
        {
            ViewBag.CnName = AppConfig.MainModule.GetCommonParameter("Parameter", "SYS_NAME");
            ViewBag.EnName = AppConfig.MainModule.GetCommonParameter("Parameter", "SYS_NAME_EN");
            return View();
        }
        [HttpPost]
        public ActionResult Valid(string usercode, string password, bool remember,string locale,string theme)
        {
            try
            {
                //移除会话
                FormsAuthentication.SignOut();
                //清除会话
                System.Web.HttpContext.Current.Session.Clear();
            }
            catch (Exception)
            {
            }
            //登录验证

            var LoginInfo = DataPortalClient.Default.Execute<ILoginService, LoginInfo>(o => o.Login(usercode, password));
            var result = LoginInfo.LoginResult;
            if (result == LoginResult.Success)
            {
                //var rUser = DataPortal.DataPortalClient.Default.Execute<ILoginService, UserPrincipal>(o => o.GetUserPrincipal(ApplicationContext.SessionID));
                //记录Cookie,SessionID
                FormsAuthentication.SetAuthCookie(LoginInfo.SessionID, remember);
                //严格说来SessionID应该保存Cookie的值
                //FormsAuthentication.GetAuthCookie(usercode, remember).Value;
                //ApplicationContext.SessionID = usercode;                
                //ApplicationContext.User = rUser;
                //语言，主题
                ApplicationContext.LocalContext["locale"] = locale;
                ApplicationContext.LocalContext["theme"] = theme;
                return Json(new { Status = "Success", Message = "登陆成功，正在跳转页面" });
            }
            else if (result == LoginResult.PasswordFail)
            {
                return Json(new { Status = "Success", Message = "帐号密码不正确" });
            }
            else if (result == LoginResult.AccountNonExist)
            {
                return Json(new { Status = "AccountNonExist", Message = "用户不存在" });
            }
            else if (result == LoginResult.AccountDisable)
            {
                return Json(new { Status = "AccountDisable", Message = "用户已禁用" });
            }
            else
                return Json(new { Status = "Fault", Message = "登陆失败" });
            //return Json(result.GetHashCode());
        }

        #region 登出
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            //退出处理
            if (System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                //移除会话
                FormsAuthentication.SignOut();

                //清除会话
                System.Web.HttpContext.Current.Session.Clear();
            }
            return RedirectToAction("Index", "Login");
        }
        #endregion
    }
}